use std::collections::BTreeSet;

pub struct Solution;
impl Solution {
    pub fn third_max(nums: Vec<i32>) -> i32 {
        let set = nums.into_iter().collect::<BTreeSet<i32>>();
        if set.len() < 3 {
            *set.last().unwrap()
        } else {
            *set.iter().rev().take(3).min().unwrap()
        }
    }
}